package com.yuanfengerp.login.service.impl;

import com.yuanfengerp.login.dao.IUserLoginDao;
import com.yuanfengerp.login.entity.User;
import com.yuanfengerp.login.service.IUserLoginService;
import com.yuanfengerp.publicUtil.JwtTokenUtil;
import com.yuanfengerp.publicUtil.PasswordUtil;
import com.yuanfengerp.publicUtil.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserLoginService {

    @Autowired
    private IUserLoginDao iuld;

    @Override
    public Result<User> login(String userName, String userPassword) {
        User userChecked = iuld.queryByUsername(userName);
        if (userChecked != null) {
            userPassword = PasswordUtil.encryptPassword(userPassword + "yuanfeng");
            if (userPassword.equals(userChecked.getUser_password())) {
                String token = new JwtTokenUtil().generateToken(userChecked.getUser_name());
                return Result.success(userChecked,"登录成功", token);
            } else {
                return Result.failure(-1, "登陆失败，密码错误");
            }
        } else {
            return Result.failure(-1, "登陆失败，请检查用户名、密码是否为空");
        }
    }
}
